package com.xjtu.chapter07.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author yjq
 * @version 1.0
 * @date 2021/8/21 17:23
 */
public class SwingComponentDemo {
    JFrame jFrame = new JFrame("测试Swing基本组件");
    //声明菜单相关组件
    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("文件");
    JMenu editMenu = new JMenu("编辑");
    JMenuItem auto = new JMenuItem("自动换行");
    JMenuItem copy = new JMenuItem("复制");
    JMenuItem paste = new JMenuItem("粘贴");
    JMenu formatMenu = new JMenu("格式");
    JMenuItem comment = new JMenuItem("注释");
    JMenuItem cancelComment = new JMenuItem("取消注释");

    //声明文本域
    JTextArea jTextArea = new JTextArea(8, 30);

    //声明列表框
    String[] colors = {"red", "blue"};
    JList<String> colorList = new JList<String>(colors);

    //声明选择相关组件
    JComboBox<String> colorSelect = new JComboBox<String>();
    ButtonGroup bg = new ButtonGroup();
    JRadioButton male = new JRadioButton("男", false);
    JRadioButton female = new JRadioButton("女", true);

    JCheckBox isMarried = new JCheckBox("是否已婚？", true);

    //声明底部
    JTextField field = new JTextField(40);
    JButton ok = new JButton("确定");

    //声明右键菜单
    JPopupMenu jPopupMenu = new JPopupMenu();
    ButtonGroup popupButton = new ButtonGroup();
    JRadioButtonMenuItem metal = new JRadioButtonMenuItem("Metal 风格");
    JRadioButtonMenuItem nimbus = new JRadioButtonMenuItem("Nimbus 风格");
    JRadioButtonMenuItem windows = new JRadioButtonMenuItem("Windows 风格", true);
    JRadioButtonMenuItem windowsClassic = new JRadioButtonMenuItem("Windows 经典风格");
    JRadioButtonMenuItem motif = new JRadioButtonMenuItem("Motif 风格");

    public void init() {
        //组装视图

        //组装底部
        JPanel bottomPanel = new JPanel();
        bottomPanel.add(field);
        bottomPanel.add(ok);

        jFrame.add(bottomPanel, BorderLayout.SOUTH);

        //组装选择相关的组件
        JPanel selectPanel = new JPanel();
        colorSelect.addItem("green");
        colorSelect.addItem("red");
        colorSelect.addItem("yellow");
        selectPanel.add(colorSelect);

        bg.add(male);
        bg.add(female);
        selectPanel.add(male);
        selectPanel.add(female);
        selectPanel.add(isMarried);

        Box topLeftBox = Box.createVerticalBox();
        topLeftBox.add(jTextArea);
        topLeftBox.add(selectPanel);
        Box top = Box.createHorizontalBox();

        //组装顶部
        top.add(topLeftBox);
        top.add(colorList);
        jFrame.add(top);

        //组装顶部菜单
        formatMenu.add(comment);
        formatMenu.add(cancelComment);
        editMenu.add(auto);
        editMenu.addSeparator();
        editMenu.add(copy);
        editMenu.addSeparator();
        editMenu.add(paste);
        editMenu.addSeparator();
        editMenu.add(formatMenu);

        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        jFrame.setJMenuBar(menuBar);

        //组装右键菜单
        popupButton.add(metal);
        popupButton.add(nimbus);
        popupButton.add(windows);
        popupButton.add(windowsClassic);
        popupButton.add(motif);

        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //当前选择的风格
                String actionCommand = actionEvent.getActionCommand();
                try {
                    changeFlavor(actionCommand);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        metal.addActionListener(listener);
        nimbus.addActionListener(listener);
        windows.addActionListener(listener);
        windowsClassic.addActionListener(listener);
        motif.addActionListener(listener);

        jPopupMenu.add(metal);
        jPopupMenu.add(nimbus);
        jPopupMenu.add(windows);
        jPopupMenu.add(windowsClassic);
        jPopupMenu.add(motif);
        //不需要再监听鼠标事件
        jTextArea.setComponentPopupMenu(jPopupMenu);

        jFrame.pack();
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口

    }

    //定义一个方法，用于改变界面风格
    private void changeFlavor(String command) throws Exception {
        switch (command) {
            case "Metal 风格":
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                break;
            case "Nimbus 风格":
                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                break;
            case "Windows 风格":
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                break;
            case "Windows 经典风格":
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
                break;
            case "Motif 风格":
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                break;
        }
        //刷新组件外观
        SwingUtilities.updateComponentTreeUI(jFrame.getContentPane());
        SwingUtilities.updateComponentTreeUI(menuBar);
        SwingUtilities.updateComponentTreeUI(jPopupMenu);
    }

    public static void main(String[] args) {
        new SwingComponentDemo().init();
    }

}
